package com.dd.controller;

import com.dd.entity.Book;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
public class BookController {

    @GetMapping("getParam")
    public String getParam(String bookName) {
        System.out.println(bookName);

        return bookName;
    }

    //get请求多个参数
    @GetMapping("getParams")
    public String getParams(String bookName, String author, double price) {
        System.out.println("普通参数传递 ==> " + bookName);
        System.out.println("普通参数传递 ==> " + author);
        System.out.println("普通参数传递 ==> " + price);

        return bookName;
    }

    //Post请求 获取form表单数据
    @PostMapping("postParam")
    public String postParam(@RequestParam("bookName") String name, String author, double price) {
        System.out.println("post请求参数 ==> " + name);
        System.out.println("普通参数传递 ==> " + author);
        System.out.println("普通参数传递 ==> " + price);

        return name;
    }

    //对象参数

    @GetMapping("getPojoParams")
    public String postPojoParams(Book book) {
        System.out.println("接收Pojo参数 ==> " + book);
        System.out.println("接收嵌套参数 ==> " + book.getType());

        return "Pojo参数接收成功";
    }


    //接收数组请求参数
    @GetMapping("arrayParams")
    public String arrayParams(String[] flowers) {
        System.out.println("接收数组参数 ===> " + Arrays.toString(flowers));

        return "数组参数接收成功";
    }

    //接收List集合请求参数
    @GetMapping("ListParams")
    public String ListParams(@RequestParam List<String> flowers) {
        System.out.println("接收List参数 ===> " + flowers);

        return "List集合参数接收成功";
    }

    //接收前端json数据
    @GetMapping("getJsonParam")
    public String jsonParam(@RequestBody Book book){
        System.out.println("接收json参数数据传递  ==> " + book);
        //接收嵌套参数
        System.out.println("接收嵌套参数数据传递 ==> " + book.getType());


        return "接收前端json数据";
    }


    // 接收json数组
    @PostMapping("postJsonParam")
    public String postJsonParam(@RequestBody List<Book> books){

        System.out.println("接收json参数数据传递  ==> " + books);

        return "接收前端json数组";
    }
}
